package com.equipment;

import java.util.NoSuchElementException;

import android.annotation.SuppressLint;
import android.util.Log;

import com.util.EncodingUtil;

/**
 * OBU卡片命令通道应答BC
 * @version 1.18
 * @author 刘亚东
 *
 */
@SuppressLint("DefaultLocale")
public class OBUCardCommandChannelResponse {
	private String STX = "55AA"; // 帧开始标志
	private String RSCTL = ""; // 串口帧序列号
	private String Length = "";// 长度
	private String code = "DF"; // 代码
	private String broadening = "BC"; // 扩展代码
	private String Status = "";// 0表示成功,非0表示失败(长度1)
	private String ChannelType=""; //1:OBU中的卡进行操作，2:对OBU中的ESAM进行操作
	private String ApduList = "";// APDU个数(长度1)
	private String[] Apdu1Length ;// APDU长度(长度1)
	private String[] Apdu1Data;// APDU应答数据，带SW(长度N)
	private String bcc = ""; // 异或校验值

	private String CardVersion = "";// 卡片密钥版本(长度1)
	private String CardNumber = "";//  卡片序列号
	private String StopTime="";    //  卡片失效期(4字节)
	private String CardVehicleNumber = "";// 卡内车牌号
	private String bingCardVehicle=""; //是否车卡绑定
	/**
	 * 接受解析BCOBU卡片命令通道应答
	 * 55AA040077DFBC000702231710110107162300000027862007100120500101000000000000002824002002000000020000000000004038240020020000000200000000000040ABA8000824180020141800200000000000000000689E0000F30000008F00000000000000613700080000000065380008EA0000001F3A0091
	 * @param context
	 * @return
	 */
	public OBUCardCommandChannelResponse analysis(String context)
			throws Exception {
		OBUCardCommandChannelResponse or = null;
		// 判断是否是BC指令
		if (!context.substring(12, 14).toUpperCase().equals("BC")) {
			throw new NoSuchElementException("OBU命令错误！");
		}
		// 判断状态是否成功
		if (!context.substring(14, 16).equals("00")) {
			throw new NoSuchElementException("OBU命令错误！");
		}
		// 截取到bcc校验值 需要判断校验是否相等
		bcc = context.substring(context.length() - 2);
		// 截取到发送的数据(总长度-2个字节计算到数据的最后位置)并转换下FE和FF
		String str = context.substring(4, context.length() - 2).toUpperCase();
		// 得到比对校验
		String bc = EncodingUtil.yihuo(EncodingUtil.change(str)).toUpperCase();
		// 判断校验是否一致
		if (!bc.equals(bcc)) {
			throw new NoSuchElementException("异或错误!");
		}
		// 如果没问题就new 对象 把解析的值放入对象
		or = new OBUCardCommandChannelResponse();
		// 解析串口帧序列号
		or.setRSCTL(context.substring(4, 6));
		// 解析长度
		or.setLength(context.substring(6, 10));
		// 解析代码类型
		or.setCode(context.substring(10, 12));
		// 解析拓展子代码
		or.setBroadening(context.substring(12, 14));
		// 截取状态
		or.setStatus(context.substring(14, 16));
		// 截取类型
		or.setChannelType(context.substring(16, 18));
		// 截取个数
		or.setApduList(context.substring(18, 20));
		int count = Integer.parseInt(context.substring(18, 20));
		Apdu1Data = new String[count];
		Apdu1Length= new String[count];
		int len = 18;
		for (int i = 0; i < count; i++) {
			// 截取数据长度
			Apdu1Length[i] = context.substring(len += 2, len += 2);
			int length = EncodingUtil.hexStringToInt(Apdu1Length[i]) * 2;
			// 截取SW
			if(length==4){
				if(!context.substring(len + length - 4 ,len +4).equals("9000")){
					Apdu1Length[i]="0";
				}
			}else{
				Log.e("CHECK：", context.substring(len + length - 4 ,len + length));
				if(!context.substring(len + length - 4 ,len + length).equals("9000")){
					Apdu1Length[i]="0";
				}
			}

			Apdu1Data[i] = context.substring(len, (len += length) - 4);
			len -= 2;
		}
		or.setApdu1Length(Apdu1Length);
		or.setApdu1Data(Apdu1Data);
		return or;
	}

	public String getSTX() {
		return STX;
	}

	public void setSTX(String sTX) {
		STX = sTX;
	}

	public String getRSCTL() {
		return RSCTL;
	}

	public String getCardVersion() {
		return CardVersion;
	}

	public void setCardVersion(String cardVersion) {
		CardVersion = cardVersion;
	}

	public void setRSCTL(String rSCTL) {
		RSCTL = rSCTL;
	}

	public String getLength() {
		return Length;
	}

	public void setLength(String length) {
		Length = length;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getBroadening() {
		return broadening;
	}

	public void setBroadening(String broadening) {
		this.broadening = broadening;
	}

	public String getStatus() {
		return Status;
	}

	public void setStatus(String status) {
		Status = status;
	}

	public String getCardVehicleNumber() {
		return CardVehicleNumber;
	}

	public void setCardVehicleNumber(String cardVehicleNumber) {
		CardVehicleNumber = cardVehicleNumber;
	}

	public String[] getApdu1Data() {
		return Apdu1Data;
	}

	public void setApdu1Data(String[] apdu1Data) {
		Apdu1Data = apdu1Data;
	}

	public String getBcc() {
		return bcc;
	}

	public void setBcc(String bcc) {
		this.bcc = bcc;
	}

	public String[] getApdu1Length() {
		return Apdu1Length;
	}

	public void setApdu1Length(String[] apdu1Length) {
		Apdu1Length = apdu1Length;
	}

	public String getApduList() {
		return ApduList;
	}

	public void setApduList(String apduList) {
		ApduList = apduList;
	}

	public String getCardNumber() {
		return CardNumber;
	}

	public void setCardNumber(String cardNumber) {
		CardNumber = cardNumber;
	}

	public String getBingCardVehicle() {
		return bingCardVehicle;
	}

	public void setBingCardVehicle(String bingCardVehicle) {
		this.bingCardVehicle = bingCardVehicle;
	}

	public String getChannelType() {
		return ChannelType;
	}

	public void setChannelType(String channelType) {
		ChannelType = channelType;
	}

	public String getStopTime() {
		return StopTime;
	}

	public void setStopTime(String stopTime) {
		StopTime = stopTime;
	}


}	
